﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKMaterialStatistic;

namespace YJKWndrStatisticsExcel.FastStatisticTable.Utility
{
	internal class ElevatorGrid
	{
        public ElevatorGrid(Autodesk.Revit.DB.Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan)
		{
			return true;
		}

		public bool GetStatisticGrid(List<Element> lstSelElems)
		{
			return true;
		}

		private void StatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			try
			{
				foreach (ViewPlan viewPlan in this.m_lstViewPlan)
				{
					ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
					Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
					transaction.Commit();
					IEnumerable<Element> enumerable = new FilteredElementCollector(this.m_doc, viewPlan.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_SpecialityEquipment).ToElements();
					int num = 0;
					foreach (Element element in enumerable)
					{
						if (!(element.Name != "电梯"))
						{
							num++;
							List<string> list = new List<string>();
							list.Insert(0, "");
							list.Insert(1, element.Name);
							list.Insert(2, "");
							list.Insert(3, "");
							list.Insert(4, "");
							list.Insert(5, "");
							list.Insert(6, "");
							list.Insert(7, "");
							list.Insert(8, "");
							list.Insert(9, "");
							this.m_dicData.Add(num.ToString(), list);
						}
					}
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(elementId);
					transaction.Commit();
				}
				if (this.m_gridSet.LinkFile)
				{
					foreach (RevitLinkInstance revitLinkInstance in this.GetAllRevitLinkInstancesInDoc())
					{
						Document linkDocument = revitLinkInstance.GetLinkDocument();
						foreach (ViewPlan viewPlan2 in this.m_lstViewPlan)
						{
							ElementId elementId2 = viewPlan2.GetUnderLayParameter().AsElementId();
							Transaction transaction2 = new Transaction(this.m_doc, "ChangeBaseLine");
							transaction2.Start();
							viewPlan2.GetUnderLayParameter().Set(ElementId.InvalidElementId);
							transaction2.Commit();
							IEnumerable<Element> enumerable2 = new FilteredElementCollector(linkDocument, viewPlan2.Id).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_SpecialityEquipment).ToElements();
							int num2 = 0;
							foreach (Element element2 in enumerable2)
							{
								if (!(element2.Name != "电梯"))
								{
									num2++;
									List<string> list2 = new List<string>();
									list2.Insert(0, "");
									list2.Insert(1, element2.Name);
									list2.Insert(2, "");
									list2.Insert(3, "");
									list2.Insert(4, "");
									list2.Insert(5, "");
									list2.Insert(6, "");
									list2.Insert(7, "");
									list2.Insert(8, "");
									list2.Insert(9, "");
									this.m_dicData.Add(num2.ToString(), list2);
								}
							}
							transaction2.Start();
							viewPlan2.GetUnderLayParameter().Set(elementId2);
							transaction2.Commit();
						}
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private void StatisticDataInSelArea()
		{
			try
			{
				int num = 0;
				foreach (Element element in this.m_lstSelElems)
				{
					if (!(element.Name != "电梯"))
					{
						num++;
						List<string> list = new List<string>();
						list.Insert(0, "");
						list.Insert(1, element.Name);
						list.Insert(2, "");
						list.Insert(3, "");
						list.Insert(4, "");
						list.Insert(5, "");
						list.Insert(6, "");
						list.Insert(7, "");
						list.Insert(8, "");
						list.Insert(9, "");
						this.m_dicData.Add(num.ToString(), list);
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private List<RevitLinkInstance> GetAllRevitLinkInstancesInDoc()
		{
			IEnumerable<ElementId> enumerable = new FilteredElementCollector(this.m_doc).OfClass(typeof(RevitLinkInstance)).ToElementIds();
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			foreach (ElementId elementId in enumerable)
			{
				Element element = this.m_doc.GetElement(elementId);
				if (!(element.GetType() != typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
					if (revitLinkInstance != null)
					{
						list.Add(revitLinkInstance);
					}
				}
			}
			return list;
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.TitleFontSize;
			int num = 2;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			Dictionary<string, List<string>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int count = this.m_dicData.Count;
			if (count == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + count + 1;
			foreach (KeyValuePair<string, List<string>> keyValuePair in this.m_dicData)
			{
				List<string> value = keyValuePair.Value;
				num++;
				for (int i = 1; i < value.Count; i++)
				{
					this.m_grid.Cell(num, i).Text = value[i];
					this.m_grid.Cell(num, i).Alignment = AlignmentEnum.CenterCenter;
				}
				this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).Sort(2, SortTypeEnum.Text, SortEnum.Ascending);
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			for (int j = 1; j < this.m_grid.Rows; j++)
			{
				this.m_grid.Row(j).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

        private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<string, List<string>> m_dicData = new Dictionary<string, List<string>>();

		private List<ViewPlan> m_lstViewPlan;

		private List<Element> m_lstSelElems;
	}
}
